import { createRouter, createWebHashHistory } from "vue-router";
import Layout from "../views/layout/Layout.vue";
import { getUser, removeToken, removeUser } from "../utils/auth.js";
import { ElNotification } from "element-plus";
const modules = (import.meta as any).glob("../views/**/index.vue");
export const constantRouterMap: any = [
  { path: "/login", component: () => import("@/views/login/index.vue"), hidden: true },
  {
    path: "/",
    single: true,
    name: "dashboard",
    component: Layout,
    hidden: false,
    redirect: "/dashboard",
    meta: { title: "dashboard", icon: "dashboard", noCache: true },

    children: [
      {
        path: "/dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        //hidden: false,
        single: true,
        meta: { title: "dashboard", icon: "dashboard", noCache: true }
      },
      //  {
      //   path: "/schedule",
      //   component: () => import("@/views/schedule/index.vue"), 
      //   single: true,
      //   meta: { title: "dashboard", icon: "dashboard", noCache: true }
      // },
      {
        path: "/account/profile",
        name: "Personal Info",
        component: () => import("@/views/account/profile.vue"),
        hidden: true,
        meta: { title: "个人资料", name: "个人资料" }
      },
      {
        path: "/account/updatePwd",
        name: "修改密码",
        component: () => import("@/views/account/updatePwd.vue"),
        hidden: true,
        meta: { title: "修改密码", name: "修改密码" }
      },
      {
        path: "/dict/index",
        name: "dictData1",
        component: () => import("@/views/basicData/dict/data.vue"),
        hidden: true,
        meta: { title: "dictData", name: "dictData1" }
      },
      {
        path: "/booking/bookingApply",
        name: "BookingApply",
        component: () => import("@/views/booking/list/apply/index.vue"),
        hidden: true,
        meta: { title: "BookingApply", name: "BookingApply" }
      },
      {
        path: "/basicData/bankInfo",
        name: "bankInfo",
        component: () => import("@/views/basicData/bankInfo/index.vue"),
        hidden: true,
        meta: { title: "bankInfo", name: "bankInfo" }
      },
      {
        path: "/contract/fixtureNote/applyFixtureNo",
        name: "ApplicationFixtureNo",
        component: () => import("@/views/contract/fixtureNote/applyFixtureNo.vue"),
        hidden: true,
        meta: { title: "ApplicationFixtureNo", name: "ApplicationFixtureNo" }
      },
      {
        path: "/contract/aiccFixtureNote",
        name: "aiccApplyNo",
        component: () => import("@/views/contract/aiccFixtureNote/index.vue"),
        hidden: false,
        meta: { title: "aiccApplyNo", name: "AiccApplyNo" }
      },
      {
        path: "/freighted/table",
        name: "freighted",
        component: () => import("@/views/freighted/table/index.vue"),
        hidden: false,
        meta: { title: "freighted", name: "freighted" }
      }, 
      {
        path: "/contract/aiccFixtureNote",
        name: "aiccFixtureNote",
        component: () => import("@/views/contract/aiccFixtureNote/index.vue"),
        hidden: false,
        meta: { title: "aiccFixtureNote", name: "aiccFixtureNote" }
      },
      {
        path: "/contract/aiccFixtureNotePreview",
        name: "aiccFixtureNotePreview",
        component: () => import("@/views/contract/aiccFixtureNotePreview/index.vue"),
        hidden: false,
        meta: { title: "aiccFixtureNotePreview", name: "aiccFixtureNotePreview" }
      },
      {
        path: "/contract/fixtureNotePreview",
        name: "fixtureNotePreview",
        component: () => import("@/views/contract/fixtureNotePreview/index.vue"),
        hidden: false,
        meta: { title: "fixtureNotePreview", name: "fixtureNotePreview" }
      },
      {
        path: "/report/revenue",
        name: "reportReRerevenue",
        component: () => import("@/views/report/revenue/index.vue"),
        hidden: false,
        meta: { title: "reportReRerevenue", name: "reportReRerevenue" }
      },
      {
        path: "/freightForward/fixtureNote",
        name: "aiccFixtureNote",
        component: () => import("@/views/freightForward/fixtureNote/index.vue"),
        hidden: false,
        meta: { title: "fixtureNote", name: "fixtureNote" }
      },   
      {
        path: "/statisticalView/index",
        name: "statisticalView",
        component: () => import("@/views/statisticalView/index.vue"),
        hidden: true,
        meta: { title: "statisticalView", name: "statisticalView" }
      },   
    ]
  },
  {
    path: "/pdfView/index",
    name: "pdfView",
    component: () => import("@/components/pdfView/index"),
    hidden: false,
    meta: { title: "pdfView", name: "pdfView" }
  },
  { path: "/404", component: () => import("@/views/404.vue"), hidden: true }
];

export const authRouter: any = () => {
  const user = getUser();
  if (!user || user.menus == null || user.menus.length == 0) {
    removeToken();
    removeUser();
    //if (location.hash.includes('token')) return []
    location.hash = "login";
    if (user?.menus?.length == 0) {
      // @ts-ignore
      ElNotification.warning({ title: "提示信息", message: "无访问权限，请联系管理员！", position: "bottom-right" });
    }
    return [];
  }
  const routers: any = [];
  const menus = user.menus;

  // const route: any = {
  //   name: 'dashboard',
  //   single: true,
  //   alwaysShow: true,
  //   component: Layout,
  //   redirect: `/dashboard`,
  //   meta: {
  //     title: 'dashboard',
  //     icon: 'dashboard'
  //   },
  //   children: []
  // }
  const route: any = {
    redirect: "/dashboard",
    component: () => import("@/views/dashboard/index.vue"),
    hidden: false,
    single: true,
    meta: { title: "dashboard", icon: "dashboard", noCache: true }
  };
  routers.push(route);
  const generateRoutes = (menuItems: any[], hidden: boolean) => {
    const routes: any[] = [];
    for (const item of menuItems) {
      const name = item.name.replace(/\//g, '');
      const path = `../views${item.comPath}/index.vue`;
      const component = modules[path];
      if (item.type == 1) {

        // 移除了 auth 相关的逻辑
        const route: any = {
          name: name,
          path: item.comPath,
          component: component || (() => import(`@/views${item.comPath}/index.vue`)),
          meta: { title: name },
          hidden: hidden
        };

        if (item.children && item.children.length > 0) {
          route.children = generateRoutes(item.children, hidden);
        }

        routes.push(route);
      }
    }
    return routes;
  };
  for (const n of menus) {
    //if (user.roleId !== 1 && n.id === 1) continue //临时屏蔽非超管角色系统管理权限
    const topMenu = n.type === 1 && n.parentId === null;
    const route: any = {
      name: n.name,
      path: n.comPath || `/${n.name}`,
      single: topMenu,
      alwaysShow: true,
      component: Layout,
      redirect: n.comPath,
      meta: {
        title: n.name,
        icon: n.icon
      },
      children: []
    };

    const hidden = n.type === 1;
    const children = n.type === 0 ? n.children : [n];

    route.children = generateRoutes(children, hidden);

    routers.push(route);
  }
  return routers;
};
export const getRouterMap: any = () => {
  const user = getUser();
  if (constantRouterMap[2].children && constantRouterMap[2].children[0]) {
    constantRouterMap[2].children[0].single = true;
    constantRouterMap[2].children[0].hidden = true;
    constantRouterMap[2].hidden = true;
  }
  return constantRouterMap;
};

//export const routes = getRouterMap().concat(authRouter())
const routes = constantRouterMap.concat(authRouter());

const router = createRouter({
  routes,
  history: createWebHashHistory(import.meta.env.VITE_APP_BASEURL)
});

export default router;
